/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;




/**
 * <p>Project: jse2204 - Stream1
 * <p>Powered by webrx On 2022-09-28 14:07:46
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Stream1 {
    public static void main(String[] args) throws Exception{



        //建立流
        Stream<Integer> s1 = Stream.of(1, 2, 3);

        Stream<Integer> s2 = List.of(1, 2, 3).stream();

        IntStream s3 = IntStream.range(1, 5);
        s3.forEach(System.out::println);

        IntStream s4 = IntStream.rangeClosed(20, 30);
        s4.forEach(System.out::println);

        IntStream s5 = IntStream.iterate(2, i -> i + 2).limit(3);
        s5.forEach(System.out::println);

        //计算1-100偶数和
        int ss = IntStream.rangeClosed(1, 100).filter(i -> i % 2 == 0).sum();
        System.out.println(ss);

        Stream<String> s6 = Stream.of("aa", "cc", "dcd", "kkvc", "kk", "cc");
        s6 = s6.filter(e -> e.contains("c"));
        System.out.println(s6.count());

        Stream<Student> s7 = Stream.of(
                new Student(1, "李四", 30),
                new Student(2, "张三", 60),
                new Student(3, "周强", 70),
                new Student(4, "周丽", 15)
        );
        s7.filter(s -> s.getScore() < 60).forEach(System.out::println);

        String src = "D:\\0824\\javase\\jse2204\\jse2204\\ch10\\src\\main\\java\\cn\\webrx\\Stream1.java";

        long rows = FileUtils.readFileToString(new File(src),"utf-8").lines().count();
        System.out.println(rows);


        //Stream<String> lines = "a\n\bc\n123\nf".lines();
        //System.out.println(lines.count());

        //Stream<Double> s8 = Stream.generate(()-> Math.random()).limit(5);
        Stream<Double> s9 = Stream.generate(Math::random).limit(5);
        //s9.forEach(System.out::println);
        s9.forEach(e->System.out.printf("%.2f%n",e));



    }
}





